<?php
/**
 * (海外仓-谷仓)库存
 */

namespace App\Logic\Plat\Rakuten\Overseas\GuCang\Inventory;


use App\Logic\Plat\Rakuten\Overseas\GuCang\RakutenOverseasClient;
use App\Models\Basics\SysPlat;
use App\Models\OverSeasBa\DropshippingInventoryFnsku;
use App\Models\Product\ProductSku;
use App\Models\Rakuten\RakutenLogs;

class OverSeasInventory extends RakutenOverseasClient{

    const get_calculate_delivery_fee = 'public_open/inventory/get_calculate_delivery_fee';
    const get_product_inventory = 'public_open/inventory/get_product_inventory';

    /**
     * 计算发货运费（发货方式）
     * @param $warehouseCode
     * @param $skuArr
     * @param $customerDeliveryOrder
     * @return mixed
     */
    public static function getCalculateDeliveryFee($warehouseCode, $skuArr, $customerDeliveryOrder){

        $apiParams = [
            'warehouse_code' => $warehouseCode, //仓库代码
            'sm_code' => null, //物流产品代码
            'country_code' => $customerDeliveryOrder->receiver->country, //目的国家/地区代码
            'postcode' => $customerDeliveryOrder->receiver->postcode, //邮政编码
            'state' => $customerDeliveryOrder->receiver->state, //省/州
            'city' => $customerDeliveryOrder->receiver->city, //城市
            'sku' => $skuArr, //商品编码:["sku1","sku2","sku3"]

            ////手动参数
            //'weight' => $params[''] ?? 0, //包裹重量(单位KG,最大保留两位小数)
            //'length' => $params[''] ?? 0, //包裹长(单位CM,最大保留两位小数)
            //'width'  => $params[''] ?? 0, //包裹宽(单位CM,最大保留两位小数)
            //'height' => $params[''] ?? 0, //包裹高(单位CM,最大保留两位小数)
            //'is_residential' => 1, //是否住宅地址(0否,1是)
            //'is_sign_server' => 1, //是否签名服务(1否,2是)
            //'is_insurance_service' => 1, //是否保险服务(1否,2是)
            //'insurance_amount' => 0, //保险金额
            //'property_label' => 'SFP', //平台模式

            'page' => 1,
            'pageSize' => 10
        ];

        $res = RakutenOverseasClient::getRequest(self::get_calculate_delivery_fee,$apiParams);
        if ($res) {
            if ($res->message == 'Success') {
                return $res;
            }else{
                //日志
                RakutenLogs::logs(RakutenLogs::$overseasOrderShippingMethod,
                    json_encode($res,JSON_UNESCAPED_UNICODE),
                    json_encode($apiParams,JSON_UNESCAPED_UNICODE)
                );
            }
        }
    }

    /**
     * 海外仓(谷仓)库存
     * @param array $skuArr
     * @return mixed
     */
    public static function getProductInventory($skuArr = []){

        $apiParams = [
            //'product_sku_arr' => $skuArr, //商品编码:["sku1","sku2","sku3"]
            'page' => 1,
            'pageSize' => 100
        ];
        if($skuArr){
            $apiParams['product_sku_arr'] = $skuArr;
        }
        
        $res = RakutenOverseasClient::getRequest(self::get_product_inventory,$apiParams);
        if ($res) {
            if ($res->message == 'Success') {
                //谷仓的所有产品库存(分页数据,合并)
                $listData = self::getResultList($res,self::get_product_inventory,$apiParams);
                if ($listData) {
                    foreach ($listData as $key => $val){
                        $pskuId = ProductSku::getValueByCode($val->product_sku,'id') ?? 0;
                        $dropshippingInventory  = DropshippingInventoryFnsku::query()
                            ->firstOrNew([
                                'account_id'   => 200000, //乐天账号ID
                                'warehouse_id' => 69, //海外仓(谷仓3号仓)ID
                                'fnsku'        => $val->product_sku,
                                'plat_id'      => SysPlat::PLAT_RAKUTEN //乐天平台ID
                            ]);
                        $dropshippingInventory->psku_id = $pskuId ?? 0;
                        $dropshippingInventory->instant = $val->sellable ? intval($val->sellable) : 0; //erp代发在库 = 谷仓可售数量
                        $dropshippingInventory->transit = $val->total_onway ? intval($val->total_onway) : 0; //erp代发在途 = 谷仓总在途数量
                        $dropshippingInventory->save();
                    }
                }
                return $res;
            }else{
                //日志
                RakutenLogs::logs(RakutenLogs::$overseasProductInventory,
                    json_encode($res,JSON_UNESCAPED_UNICODE),
                    json_encode($apiParams,JSON_UNESCAPED_UNICODE)
                );
            }
        }
    }

}
